package com.kou.dao;

import com.kou.pojo.Type;
import com.kou.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author SteinY
 * @create 2022-03-25 11:22
 * @description:
 */
public class TypeDaoImpl implements  TypeDao {
   public Connection connection = DataSourceUtil.getConnection();
    public QueryRunner queryRunner = new QueryRunner();

    @Override
    public List<Type> findAll() {
        String sql = "select * from tb_goods_type where level = 1";
        try {
            return queryRunner.query(connection, sql, new BeanListHandler<>(Type.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Type> select(String level, String goodsname) {
        String sql = "select * from tb_goods_type where level = ? and name like '%"+goodsname+"%'";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Type.class),Integer.parseInt(level));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Type> findSon(String id) {
        String sql = "select * from tb_goods_type where parent = ? ";
        try {
            return queryRunner.query(connection,sql,new BeanListHandler<>(Type.class),Integer.parseInt(id));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void delete(String id) {
        String sql = "delete from tb_goods_type where id = ?";
        try {
            queryRunner.update(connection,sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    @Override
    public void create(int parent, int i, String typename) {
        String  sql = "insert into tb_goods_type values (0,?,?,?) ";
        try {
            queryRunner.update(connection,sql,typename,i,parent);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int findLevel(int parseInt) {
        String sql = "select id from tb_goods_type where id = ? ";
        try {
            return (Integer) queryRunner.query(connection,sql,new ScalarHandler(),parseInt);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Type> findAll1() {
        String sql = "select * from tb_goods_type ";
        try {
            return queryRunner.query(connection, sql, new BeanListHandler<>(Type.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Type find(int id) {
        String sql = "select * from tb_goods_type where id = ? ";
        try {
            return queryRunner.query(connection,sql,new BeanHandler<>(Type.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void update(Type type) {
        if (type.getLevel()!=null){
            String sql = "update tb_goods_type set name = ? , level = ? , parent = ? where id = ?";
            Object params[] = {type.getName(),type.getLevel(),type.getParent(),type.getId()};
            try {
                queryRunner.update(connection,sql,params);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }else {
            String sql = "update tb_goods_type set name = ? where id = ?";
            Object params[] = {type.getName(),type.getId()};
            try {
                queryRunner.update(connection,sql,params);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
